package com.ls.hnks.component;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ls.hnks.model.User;
import com.ls.hnks.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 自定义权限验证接口扩展
 */
@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserService userService;


    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("`right`");
        queryWrapper.eq("id",loginId);
        Map<String, Object> map = userService.getMap(queryWrapper);
        String  right = map.get("right").toString();
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        List<String> list = new ArrayList<String>();
        list.add(right.equals("1") ? "admin" : "user");
        log.info("用户角色 --- {}",list);
        return list;
    }

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        return new ArrayList<String>();
    }

}